package com.bora_bandeco;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;

public class SplashActivity extends FragmentActivity  {
	private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		finish();
		// Check device for Play Services APK.
	    if (!checkPlayServices()) {
	        // If this check succeeds, proceed with normal processing.
	        // Otherwise, prompt user to get valid Play Services APK.
	    	System.out.println("This device is not supported.");
	        return;
	    }
		
		SharedPreferences mSharedPreferences = getSharedPreferences("com.bora_bandeco", Context.MODE_PRIVATE);

		// Se o registro já foi realizado e as preferências definidas 
		// ir direto para o cardápio
        if (!mSharedPreferences.getString("nome", "").equals("")) {
        	Intent intent;
			intent = new Intent(this, CardapioActivity.class);
	        startActivity(intent);
        } else {
        	Intent intent;
			intent = new Intent(this, RegistroActivity.class);
	        startActivity(intent);
        }
	}
	
	// You need to do the Play Services APK check here too.
	@Override
	protected void onResume() {
	    super.onResume();
	    checkPlayServices();
	}
	
	private boolean checkPlayServices() {
	    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
	    if (resultCode != ConnectionResult.SUCCESS) {
	        if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
	            GooglePlayServicesUtil.getErrorDialog(resultCode, this, PLAY_SERVICES_RESOLUTION_REQUEST).show();
	        } else {
	            System.out.println("This device is not supported.");
	            finish();
	        }
	        return false;
	    }
	    return true;
	}
}
	